﻿using System;
using System.Xml;

using octalforty.Syndication.Core;
using octalforty.Syndication.Core.Resources;

namespace octalforty.Syndication.Core.Rss.Aggregators
{
    /// <summary>
    /// Aggregates RSS <c>&lt;channel&gt;</c> element.
    /// </summary>
    public class RssChannelAggregator : RssAggregatorBase
    {
        /// <summary>
        /// Initializes a new instance of <see cref="RssChannelAggregator"/> class.
        /// </summary>
        public RssChannelAggregator()
        {
        }

        #region RssAggregatorBase Members
        /// <summary>
        /// Internal method, invoked from the implementation of 
        /// <see cref="RssAggregatorBase.InternalAggregate(XmlDocument,ref ISyndicationFeed)"/>.
        /// </summary>
        /// <param name="feedDocument"></param>
        /// <param name="rssFeed"></param>
        /// <remarks>
        /// The implementation must not invoke <see cref="IAggregator.Aggregate"/> 
        /// on the <see cref="AggregatorBase.NextAggregator"/>.
        /// </remarks>
        protected override void InternalAggregate(XmlDocument feedDocument, ref RssFeed rssFeed)
        {
            //
            // Loading channel data. Start off with required channel elements.
            rssFeed.Title = GetNodeInnerText(feedDocument, "/rss/channel/title", 
                Strings.RssChannelAggregator_MissingRssChannelTitle);
            rssFeed.Link = GetNodeInnerText(feedDocument, "/rss/channel/link",
                Strings.RssChannelAggregator_MissingRssChannelLink);
            rssFeed.Description = GetNodeInnerText(feedDocument, "/rss/channel/description",
                Strings.RssChannelAggregator_MissingRssChannelDescription);

            //
            // Now proceed with optional elements.
            rssFeed.Channel.Language = GetNodeInnerText(feedDocument, "/rss/channel/language");
            rssFeed.Channel.Copyright = GetNodeInnerText(feedDocument, "/rss/channel/copyright");
            rssFeed.Channel.ManagingEditor =
                new RssPerson(string.Empty, GetNodeInnerText(feedDocument, "/rss/channel/managingEditor"));
            rssFeed.Channel.WebMaster =
                new RssPerson(string.Empty, GetNodeInnerText(feedDocument, "/rss/channel/webMaster"));

            if(GetNodeInnerText(feedDocument, "/rss/channel/pubDate") != string.Empty)
                rssFeed.PublishedOn =
                    ParseRfc1123DateTime(GetNodeInnerText(feedDocument, "/rss/channel/pubDate"));

            if(GetNodeInnerText(feedDocument, "/rss/channel/lastBuildDate") != string.Empty)
                rssFeed.UpdatedOn = 
                    ParseRfc1123DateTime(GetNodeInnerText(feedDocument, "/rss/channel/lastBuildDate"));

            rssFeed.Channel.Generator = GetNodeInnerText(feedDocument, "/rss/channel/generator");

            if(GetNodeInnerText(feedDocument, "/rss/channel/ttl") != string.Empty)
                rssFeed.Channel.TimeToLive = TimeSpan.FromMinutes(
                    double.Parse(GetNodeInnerText(feedDocument, "/rss/channel/ttl")));
        }
        #endregion
    }
}
